import collections
import functools
class Solution(object):
    def checkIfPrerequisite(self, numCourses, prerequisites, queries):
        d = collections.defaultdict(set)
        for u, v in prerequisites:
            d[u].add(v)
        @functools.cache
        def dfs(u, v):
            if v in d[u]:
                return True
            else:
                for node in d[u]:
                    if dfs(node, v):
                        return True
                return False
        out = []
        for u, v in queries:
            out.append(dfs(u, v))
        return out



